package com.demo.yuanzi.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.demo.yuanzi.entity.req.LoginUser;
import com.demo.yuanzi.service.YuanziSaUserService;
import com.demo.yuanzi.util.YuResult;

import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/auth/user/")
public class UserController {
    @Resource
    private YuanziSaUserService yuanziSaUserService;
    @RequestMapping("/doLogin")
    public YuResult<SaTokenInfo> doLogin(@RequestBody LoginUser loginUser) {
        try {
            if (log.isInfoEnabled()){
                log.info("用户名为:{},密码为;{}",loginUser.getUsername(),loginUser.getPassword());
            }
            Preconditions.checkNotNull(loginUser.getUsername(),"用户名不能为空");
            Preconditions.checkNotNull(loginUser.getPassword(),"密码不能为空");
            return YuResult.ok(yuanziSaUserService.login(loginUser.getUsername(),loginUser.getPassword()));
        }catch (Exception e){
            log.info("登录失败，失败信息为:{}",e.getMessage(),e);
            return  YuResult.fail("登录失败");
        }



    }

}
